﻿using System;
using System.Collections.Generic;
using System.Linq;
using VoicePhone.Common;
using System.IO;
using System.Windows;
using System.Xml.Serialization;

namespace VoicePhone.Utils
{
    public class CallRecordsConfig : EntityClassXmlSerializer<CallRecordsConfig>
    {
        public List<CdrInfo> CdrList { get; set; }

        public static CallRecordsConfig Load()
        {
            CallRecordsConfig CallRecordsConfig;
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config\\CallRecordsConfig.xml");
            if (File.Exists(filePath))
            {
                string xmlStr = File.ReadAllText(filePath);
                CallRecordsConfig = ReadFromXML(xmlStr);
            }
            else
            {
                CallRecordsConfig = new CallRecordsConfig();
            }

            if (CallRecordsConfig.CdrList == null)
                CallRecordsConfig.CdrList = new List<CdrInfo>();

            return CallRecordsConfig;
        }

        public void Save()
        {
            this.Save(ToXMLString(this), "CallRecordsConfig.xml");
        }
    }

    public class CdrInfo : DependencyObject
    {
        public CdrInfo() { }
        //public CdrInfo(Cdr cdr)
        //{
        //    this.TimeStart = cdr.TimeStart;
        //    this.Route = cdr.Route;
        //    this.Recording = cdr.Recording;
        //    this.TimeEnd = cdr.TimeEnd;
        //    this.Duration = cdr.Duration;
        //    this.CPN = cdr.CPN;
        //    this.CDPN = cdr.CDPN;
        //}

        private string m_TimeStart;
        public string TimeStart
        {
            get { return m_TimeStart; }
            set
            {
                if (!string.IsNullOrWhiteSpace(value))
                {
                    m_TimeStart = DateTimeHelper.LongToDateTimeString(value);
                }
            }
        }


        public string Route { get; set; }

        public string CPN { get; set; }

        public string CDPN { get; set; }

        public string CallNo
        {
            get
            {
                return CallType == "监视" ? this.CPN + "-->" + this.CDPN : CPN == ShareInfo.SettingConfig.AppServer.LocalCallNo ? CDPN : CPN;
            }
        }

        public string CallType
        {
            get
            {
                return CPN != ShareInfo.SettingConfig.AppServer.LocalCallNo && CDPN != ShareInfo.SettingConfig.AppServer.LocalCallNo ? "监视" : CPN == ShareInfo.SettingConfig.AppServer.LocalCallNo ? "去电" : "来电";
            }
        }

        public string CallName
        {
            get
            {
                if (CallType == "监视")
                {
                    return ShareInfo.NumberThinConfig.IpPhoneList.FirstOrDefault(p => p.CallNo1 == this.CPN)?.CallName +
                          "-->" + ShareInfo.NumberThinConfig.IpPhoneList.FirstOrDefault(p => p.CallNo1 == this.CDPN)?.CallName;
                }
                return ShareInfo.NumberThinConfig.IpPhoneList.FirstOrDefault(p => p.CallNo1 == (CallType == "去电" ? this.CDPN : this.CPN))?.CallName;
            }
        }

        public string TimeEnd { get; set; }

        public int Duration { get; set; }

        public string Recording { get; set; }

        [XmlIgnore]
        public string PlayState
        {
            get { return (string)GetValue(PlayStateProperty); }
            set { SetValue(PlayStateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PlayState.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PlayStateProperty =
            DependencyProperty.Register("PlayState", typeof(string), typeof(CdrInfo), new PropertyMetadata("停止"));

        public int Index
        {
            get { return (int)GetValue(IndexProperty); }
            set { SetValue(IndexProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Index.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IndexProperty =
            DependencyProperty.Register("Index", typeof(int), typeof(CdrInfo), new PropertyMetadata(0));
    }
}
